﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entities;
namespace Data
{
   public class TacGiaRepository
    {
       public static List<TacGiaDTO> GetTacGiacList()
       {           
           NhaSachEntities context = new NhaSachEntities();
           var q = from tg in context.TacGiaList
                   select new TacGiaDTO
                   {
                       MaTacGia=(int)tg.MaTacGia,
                       TenTacGia=tg.TenTacGia,
                       TinhTrang=(int)tg.TinhTrang
                   };
           return q.ToList();
       }
       public static TacGiaDTO GetTacGiaByMaTacGia(int maTacGia)
       {
           NhaSachEntities context = new NhaSachEntities();
           var q = (from tg in context.TacGiaList
                   where tg.MaTacGia == maTacGia
                   select new TacGiaDTO()
                   {
                       MaTacGia = (int)tg.MaTacGia,
                       TenTacGia = tg.TenTacGia,
                       TinhTrang = (int)tg.TinhTrang
                   }).FirstOrDefault();
           return q;
       }

       public static int GetCurrentID()
       {
           NhaSachEntities context=new NhaSachEntities();

           Nullable<int> id = context.GetCurrentID("TacGia").SingleOrDefault();
           return (int)id;
       }
       public static bool CheckDelete(int maTacGia)
       {
           NhaSachEntities context = new NhaSachEntities();
           var sach = (from s in context.SachList
                       where s.MaTacGia == maTacGia
                       select s).Count();
           if (sach > 0)
               return false;
           return true;
       }
       public static void Delete(int maTacGia)
       {
           NhaSachEntities context=new NhaSachEntities();
           var tacGia=(from tg in context.TacGiaList
                      where tg.MaTacGia==maTacGia
                      select tg).FirstOrDefault();
           context.DeleteObject(tacGia);
           context.SaveChanges();
       }
       public static void Update(TacGiaDTO tacGia)
       {
           NhaSachEntities context = new NhaSachEntities();
           var q = (from tg in context.TacGiaList
                   where tg.MaTacGia == tacGia.MaTacGia
                   select tg).FirstOrDefault();
           q.TinhTrang = tacGia.TinhTrang;
           q.TenTacGia = tacGia.TenTacGia;
           context.SaveChanges();
       }
       public static void Insert(TacGiaDTO tacGia)
       {
           TacGia tg = new TacGia()
           {
               TenTacGia = tacGia.TenTacGia,
               TinhTrang = tacGia.TinhTrang
           };
           NhaSachEntities context = new NhaSachEntities();
           context.AddToTacGiaList(tg);
           context.SaveChanges();
       }
    }
}
